package com.lebang.lechuangsystem.controller.sys;

import com.lebang.lechuangsystem.common.SessionUtil;
import com.lebang.lechuangsystem.domain.Permission;
import com.lebang.lechuangsystem.domain.User;
import com.lebang.lechuangsystem.model.PageBean;
import com.lebang.lechuangsystem.po.UserPo;
import com.lebang.lechuangsystem.service.sys.RoleService;
import com.lebang.lechuangsystem.service.sys.UserService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.encrypt.Digests;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author: hwq
 * @Date: 2019/8/22 14:04
 * @Version 1.0
 */
@Api(tags = "<后台管理> 用户管理")
@RestController
@RequestMapping("/sys/user")
public class UserController {
    @Autowired
    private UserService baseService;
    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public JsonData add(User vo) throws Exception {
        vo.setPassword(Digests.md5Str(vo.getPassword().getBytes()));
        vo.setCreatedTime(new Date());
        return baseService.insertModel(vo) > 0 ? Callback.success("200", "操作成功") : Callback.fail("200", "操作失败");
    }
    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public JsonData update(User vo) {
        vo.setPassword(Digests.md5Str(vo.getPassword().getBytes()));
        vo.setUpdatedTime(new Date());
        return baseService.updateById(vo) > 0 ? Callback.success("200", "操作成功") : Callback.fail("200", "操作失败");
    }
    @ApiOperation(value = "根据id获取详情")
    @GetMapping("/get/{id}")
    public JsonData get(@PathVariable Integer id, HttpServletRequest request) {
        return Callback.success("200", "查询成功", baseService.selectById(id));
    }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/delete/{id}")
    public JsonData delete(@PathVariable Integer id, HttpServletRequest request) {
        return baseService.deleteById(id) > 0 ? Callback.success("200", "操作成功") : Callback.fail("200", "操作失败");
    }

    @ApiOperation(value = "分页")
    @PostMapping("/list")
    public JsonData list(HttpServletRequest request,
                         @RequestParam Integer page,
                         @RequestParam Integer limit,
                         @RequestParam(required = false) Integer roleId,
                         @RequestParam(required = false) String username,
                         @RequestParam(required = false) Integer status,
                         @RequestParam(required = false) String truename) {
        UserPo example = new UserPo();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        UserPo.Criteria criteria = example.createCriteria();
        if(!TextUtil.isEmpty(roleId)){
            criteria.andRoleIdEqualTo(roleId);
        }
        if(!TextUtil.isEmpty(username)){
            criteria.andUsernameEqualTo(username);
        }
        if(!TextUtil.isEmpty(status)){
            criteria.andStatusEqualTo(status);
        }
        if(!TextUtil.isEmpty(truename)){
            criteria.andTrueNameLike("%"+truename+"%");
        }
        PageBean<User> pageBean = baseService.pageByExample(example);
        return Callback.success("200", "查询成功", pageBean);
    }

    @ApiOperation(value = "获取当前用户所有菜单")
    @PostMapping("/permission/list")
    public JsonData rolePermission(HttpServletRequest request, @RequestParam Integer roleId, HttpSession session) {
        Integer id= SessionUtil.getSessionId();
        User user=baseService.selectById(id);
        //超级管理员默认全部菜单
        if(user.getRole().getType().equals(0)){
            user.setRoleId(null);
        }
        List<Permission> permissions = roleService.selectPermissionListByRoleId(user.getRoleId());
        return Callback.success("200", "查询成功", permissions);
    }
}
